import os
import time
import pyaudio
import wave
import audioop
import time
import pygame as pg
import numpy as np

#################
#  Sound Ghost  #
#################

class SoundGhost():

    # def __init__(self):
    
    def sg_compile(self, name):
        try:
            os.system("python jianpu-ly.py <%s> %s.ly" % (name, name))
            os.system("lilypond %s.ly" % name)
            return True
        except Exception, e:
            print e
            return False

    def play_midi(self, name):
        try:
            pg.init()
            pg.mixer.music.load("%s.midi" % name)
            pg.mixer.music.play()
            return True
        except Exception, e:
            print e
            return False

    def stop_midi(self):
        try:
            pg.mixer.music.stop()
            return True
        except Exception, e:
            print e
            return False

    notes = {
        "1": 262,
        "1#": 277,
        "2": 294,
        "2#": 311,
        "3": 330,
        "4": 349,
        "4#": 370,
        "5": 392,
        "5#": 415,
        "6": 440,
        "6#": 466,
        "7": 494
    }
    
    def findNote(freq):
        delta = 1000
        closest = "1"
        for key in notes:
            if freq % notes[key] < freq % notes[closest]:
                closest = key
        return closest

    def record_find_note():
        CHUNK = 2048
        FORMAT = pyaudio.paInt16
        CHANNELS = 1
        RATE = 44100
        RECORD_SECONDS = 5
        WAVE_OUTPUT_FILENAME = "output.wav"
        p = pyaudio.PyAudio()
        stream = p.open(format=FORMAT,
                        channels=CHANNELS,
                        rate=RATE,
                        input=True,
                        frames_per_buffer=CHUNK)
        print("* recording")
        frames = []

        window = np.blackman(CHUNK)

        while True:
            data = stream.read(CHUNK)
            rms = audioop.rms(data, 2)
            volumn = 20 * np.log10(rms)
            indata = np.array(wave.struct.unpack("%dh" % (len(data) / 2), data)) * window
            fftData = abs(np.fft.rfft(indata)) ** 2
            which = fftData[1:].argmax() + 1

            if volumn > 65:
                y0, y1, y2 = np.log(fftData[which - 1:which + 2:])
                x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0)
                # find the frequency and output it
                thefreq = (which + x1) * RATE / CHUNK
                print "The freq is %f Hz." % (thefreq)
                note = findNote(thefreq)
                print note
